package com.example.ssm.controller;

import com.example.ssm.context.PaymentContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 支付上下文控制器
 * 演示使用PaymentContext自动选择支付策略
 */
@RestController
@RequestMapping("/api/payment-context")
public class PaymentContextController {

    @Autowired
    private PaymentContext paymentContext;

    /**
     * 处理支付请求，自动识别支付方式
     * @param paymentAccount 支付账号（支付宝账号、微信支付标识或银行卡号）
     * @param amount 支付金额
     * @param orderId 订单ID
     * @return 支付结果
     */
        @PostMapping("/auto-pay")
    public Map<String, Object> autoPay(
            @RequestParam String paymentAccount,
            @RequestParam BigDecimal amount,
            @RequestParam String orderId) {

        String result = paymentContext.processPayment(paymentAccount, amount, orderId);

        Map<String, Object> response = new HashMap<>();
        response.put("success", !result.contains("失败") && !result.contains("无法识别"));
        response.put("message", result);
        response.put("orderId", orderId);
        response.put("amount", amount);
        response.put("paymentAccount", paymentAccount);

        return response;
    }

    /**
     * 演示不同类型账号的自动识别
     * @return 演示结果
     */
    @GetMapping("/demo")
    public Map<String, Object> demo() {
        Map<String, Object> result = new HashMap<>();
        List<Map<String, Object>> paymentResults = new ArrayList<>();

        // 模拟订单信息
        String orderId = "ORDER" + System.currentTimeMillis();
        BigDecimal amount = new BigDecimal("100.50");

        // 支付宝账号（邮箱格式）
        Map<String, Object> alipayEmailResult = new HashMap<>();
        alipayEmailResult.put("accountType", "支付宝账号（邮箱格式）");
        alipayEmailResult.put("account", "user@example.com");
        alipayEmailResult.put("result", paymentContext.processPayment("user@example.com", amount, orderId));
        paymentResults.add(alipayEmailResult);

        // 支付宝账号（手机号格式）
        Map<String, Object> alipayPhoneResult = new HashMap<>();
        alipayPhoneResult.put("accountType", "支付宝账号（手机号格式）");
        alipayPhoneResult.put("account", "13812345678");
        alipayPhoneResult.put("result", paymentContext.processPayment("13812345678", amount, orderId));
        paymentResults.add(alipayPhoneResult);

        // 微信支付标识
        Map<String, Object> wechatResult = new HashMap<>();
        wechatResult.put("accountType", "微信支付标识");
        wechatResult.put("account", "wx_123456789abcdef0");
        wechatResult.put("result", paymentContext.processPayment("wx_123456789abcdef0", amount, orderId));
        paymentResults.add(wechatResult);

        // 银行卡号
        Map<String, Object> bankCardResult = new HashMap<>();
        bankCardResult.put("accountType", "银行卡号");
        bankCardResult.put("account", "6225123456789012");
        bankCardResult.put("result", paymentContext.processPayment("6225123456789012", amount, orderId));
        paymentResults.add(bankCardResult);

        // 无法识别的账号格式
        Map<String, Object> unknownResult = new HashMap<>();
        unknownResult.put("accountType", "无法识别的账号格式");
        unknownResult.put("account", "unknown-format");
        unknownResult.put("result", paymentContext.processPayment("unknown-format", amount, orderId));
        paymentResults.add(unknownResult);

        // 使用Map参数方式
        Map<String, Object> params = new HashMap<>();
        params.put("account", "13987654321");
        params.put("amount", amount);
        params.put("orderId", orderId);

        Map<String, Object> mapParamResult = new HashMap<>();
        mapParamResult.put("accountType", "使用Map参数方式");
        mapParamResult.put("account", params.get("account"));
        mapParamResult.put("result", paymentContext.processPayment(params));
        paymentResults.add(mapParamResult);

        result.put("success", true);
        result.put("message", "支付上下文自动识别演示");
        result.put("results", paymentResults);

        return result;
    }
}
